﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;

namespace SNSpirit.Documents
{
	/// <summary>
	/// Offer some extension method for <see cref="ITypedDocumentAdatper"/> interface. This is a static class.
	/// </summary>
	public static class TypedDocumentAdapterExtensions
	{
		/// <summary>
		/// Convert a <see cref="TypedDocument"/> to another type.
		/// </summary>
		/// <param name="adapter">The adapter to take the conversion.</param>
		/// <param name="source">The source document and type information.</param>
		/// <param name="targetType">The target type to be converting.</param>
		/// <returns>The new document with the target type.</returns>
		public static TypedDocument ConvertAndWrap(this ITypedDocumentAdapter adapter, TypedDocument source, string targetType)
		{
			if (adapter == null)
			{
				throw new ArgumentNullException("adapter");
			}

			if (source == null)
			{
				throw new ArgumentNullException("source");
			}

			if (targetType == null)
			{
				throw new ArgumentNullException("targetType");
			}

			// Need of code contracts.
			Contract.EndContractBlock();

			var result = adapter.Convert(source, targetType);

			return new TypedDocument(result, targetType);
		}
	}
}
